package io;

import sun.security.provider.MD5;

import java.io.*;
import java.util.Properties;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-25 11:14
 * @version: 1.0
 */
public class ReadProperties {
    private String name;
    private int id;
    static Properties properties = null;
    static FileInputStream inputStream = null;
    private static final String url = ".\\test.properties";

    static {
        properties = new Properties();
        try {
            inputStream = new FileInputStream(new File(url));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ReadProperties() {
        this.id = Integer.parseInt(properties.getProperty("id"));
        this.name = properties.getProperty("name");
    }

    public void updateAndAdd() {
        properties.setProperty("name", "giao");
        properties.setProperty("pwd", "123456");
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(url);
            properties.store(outputStream, "vvtest.properties");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void get(){
        //获取系统配置
        Properties properties1 = System.getProperties();
        properties1.list(System.out);
    }

    public static void main(String[] args) {
        ReadProperties r = new ReadProperties();
        System.out.println("r.name = " + r.name);
        System.out.println("r.id = " + r.id);

        r.updateAndAdd();

        r.get();
    }
}
